package putao.ui
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import putao.ui.assets.Assets;
	import putao.ui.events.PSelectButtonEvent;

	[Event(name="P_SELECT", type="putao.ui.events.PSelectButtonEvent")]
	
	public class PSelectButton extends PButton
	{
		//按钮背景
		protected var sSelectedBtnDafult:DisplayObject;
		protected var sSelectedBtnRollover:DisplayObject;
		protected var sSelectedBtnPressed:DisplayObject;
		protected var sSelectedBtnDisabled:DisplayObject;
		
		private var _isSelected:Boolean = false;
		
		public function PSelectButton(text:String="")
		{
			super(text);
		}
		
		public function get isSelected():Boolean
		{
			return _isSelected;
		}

		public function set isSelected(value:Boolean):void
		{
			//当isSelected的值变化时，改编按钮的选择状态
			if(_isSelected != value){
				_isSelected = value;
				changeShowImg(currentState);
			}
		}
		
		public function select():void
		{
			isSelected = true;
		}
		
		public function unSelect():void
		{
			isSelected = false;
		}

		/**
		 * 设置按钮在按下状态下的皮肤
		 * 结合setSkin方法才能重置所有的皮肤 
		 *  
		 * @param btnDafult 默认状态
		 * @param btnRollover 鼠标移上
		 * @param btnPressed 按下
		 * @param btnDisabled 无效
		 * 
		 */		
		public function setSelectedSkin(btnDafult:DisplayObject, btnRollover:DisplayObject, btnPressed:DisplayObject, btnDisabled:DisplayObject):void
		{
			sSelectedBtnDafult = btnDafult;
			sSelectedBtnRollover = btnRollover;
			sSelectedBtnPressed = btnPressed;
			sSelectedBtnDisabled = btnDisabled;
		}
		
		override protected function initDefaultSkin():void
		{
			// TODO Auto Generated method stub
			super.initDefaultSkin();
			
			sSelectedBtnDafult = Assets.getDisplayObject(Assets.BtnPressed);
			sSelectedBtnRollover = Assets.getDisplayObject(Assets.BtnPressed);
			sSelectedBtnPressed = Assets.getDisplayObject(Assets.BtnPressed);
			sSelectedBtnDisabled = Assets.getDisplayObject(Assets.BtnDisabled);
		}
		
		
		override protected function onMouse(e:MouseEvent):void
		{
			super.onMouse(e);
			
			switch(e.type)
			{
				case MouseEvent.MOUSE_UP:
					if(isOver){
						isSelected = !isSelected;
						if(isSelected)
						{
							//按钮被选中后发送事件
							dispatchEvent(new PSelectButtonEvent(PSelectButtonEvent.SELECT, true, false));
						}
					}
					break;
			}
		}
		
		override public function doSpaceUp():void
		{
			super.doSpaceUp();
			
			isSelected = !isSelected;
			if(isSelected)
			{
				//按钮被选中后发送事件
				dispatchEvent(new PSelectButtonEvent(PSelectButtonEvent.SELECT, true, false));
			}
		}
		
		
		override public function resize(width:Number, height:Number):void
		{
			super.resize(width, height);
			var arr:Array = [sSelectedBtnDafult, sSelectedBtnRollover, sSelectedBtnPressed, sSelectedBtnDisabled];
			for(var i:int = 0; i < arr.length; i++){
				arr[i].width = width;
				arr[i].height = height;
			}
		}
		
		override protected function changeShowImg(state:String):void
		{
			this.removeChild(currentDisObj);
			this.currentState = state;
			if(_isSelected == false){
				currentDisObj = getDisObj(state);
			}else{
				currentDisObj = getSelectedDisObj(state);
			}
			this.addChildAt(currentDisObj, 0);
		}
		
		private function getSelectedDisObj(state:String):DisplayObject
		{
			switch(state)
			{
				case BTN_UP:
					return sSelectedBtnDafult;
					break;
				case BTN_DISABLED:
					return sSelectedBtnDisabled;
					break;
				case BTN_PRESSED:
					return sSelectedBtnPressed;
					break;
				case BTN_OVER:
					return sSelectedBtnRollover;
					break;
			}
			return sSelectedBtnDafult;
		}
	}
}